<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs>
          <el-tab-pane label="角色管理" name="first">
            <el-row>
              <el-button type="primary" size="small" @click="isShowDialog = true">
                +新增角色
              </el-button>
            </el-row>
            <el-table
              :data="roleList"
              style="width: 100%"
            >
              <el-table-column
                prop="name"
                label="角色名称"
                width="180"
              />
              <el-table-column
                prop="description"
                label="描述"
                width="180"
              />
              <el-table-column
                prop="address"
                label="操作"
              >
                <template v-slot="scoped">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editList(scoped.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delList(scoped.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <el-row>
              <el-pagination
                :page-size="pageSetting.pagesize"
                layout="total, prev, pager, next"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称" style="width: 400px">
                <el-input v-model="companyInfo.name" disabled />
              </el-form-item>
              <el-form-item label="公司地址" style="width: 400px">
                <el-input v-model="companyInfo.companyAddress" disabled />
              </el-form-item>
              <el-form-item label="邮箱" style="width: 400px">
                <el-input v-model="companyInfo.mailbox" disabled />
              </el-form-item>
              <el-form-item label="备注" style="width: 400px">
                <el-input v-model="companyInfo.remarks" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 制作对话框 -->
      <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnCancel">
        <!-- 对话框内制作表单 -->
        <el-form ref="formData" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
        </el-form>
        <!-- 对话框组件默认有个footer插槽 -->
        <template #footer>
          <el-button type="pramary" @click="submitData">确定</el-button>
          <el-button type="danger" @click="btnCancel">取消</el-button>
        </template>

      </el-dialog>
    </div>

  </div>
</template>

<script>
import { companyInfo, getRoleList, addRoleList, editRoleList, updataRoleList, delRoleList } from '@/api/setting'
export default {
  data() {
    return {
      companyInfo: {},
      roleList: [],
      pageSetting: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      isShowDialog: false,
      // 弹出框中表单数据
      formData: {
        name: '',
        description: ''
      },
      // 表单检验
      rules: {
        name: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '名字长度需要在2-6之间', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
          { min: 5, max: 15, message: '描述长度需要在5-15之间', trigger: 'blur' }
        ]
      }

    }
  },

  async created() {
    // 进入页面获取公司信息
    const res = await companyInfo(this.$store.state.user.userInfo.companyId)
    this.companyInfo = res
    // 进入页面获取角色列表信息
    this.getRoleList()
  },
  methods: {
    async delList(id) {
      // 发起删除请求
      await delRoleList(id)
      // 更新页面
      this.getRoleList()
      // 提示删除成功
      this.$message.success('删除成功')
    },
    // 点击编辑时弹出对话框并获取数据渲染表单
    async editList(id) {
      // 点击编辑打开对话框
      this.isShowDialog = true
      // 根据id发送请求获取当前角色信息
      console.log(id)
      this.formData = await editRoleList(id)
      console.log('this.formData', this.formData)
      // console.log('1', this.formData.id)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    async handleCurrentChange(newPage) {
      // console.log(`当前页: ${val}`)
      this.pageSetting.page = newPage
      // 重新再获取角色列表
      this.getRoleList()
    },
    // 获取角色列表
    async getRoleList() {
      const res1 = await getRoleList(this.pageSetting)
      this.total = res1.total
      this.roleList = res1.rows
    },
    // 点击确定进行兜底校验并发送新增请求和更新请求
    async submitData() {
      try {
        await this.$refs.formData.validate()
        console.log('1', this.formData.id)
        if (this.formData.id) {
          // 发送更新角色请求
          await updataRoleList(this.formData)

          // 提醒用户
          this.$message.success('更新成功')
        } else {
          // 发送新增角色请求
          const res = await addRoleList(this.formData)
          console.log(res)
          // 提醒用户
          this.$message.success('添加成功')
        }

        // 关闭弹窗
        this.isShowDialog = false

        // 更新页面
        this.getRoleList()
        // 数据清空
        this.formData = {
          name: '',
          description: ''

        }
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮的操作
    btnCancel() {
      // 点击关闭弹窗
      this.isShowDialog = false
      // 手动清除数据
      this.formData = {
        name: '',
        description: ''
      }
      // 重置数据并清除检验错误
      this.$refs.formData.resetFields()
    }
  }
}

</script>

<style>

</style>
